package com.gimi.cloud.bbp.client.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.client.service.page.PageConfigService;
import com.gimi.cloud.bbp.common.constant.RedisKeyConstant;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.commonService.redis.RedisService;
import com.gimi.cloud.bbp.dto.page.PageConfigDTO;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

@RestController
public class PageConfigController {
	
	@Autowired private PageConfigService pageConfigService;
	@Autowired private RedisService redisService;
	
	private final static Logger log = LoggerFactory.getLogger(PageConfigController.class);
	
	/**
	 * 获取首页专场
	 * @param dto
	 * @param response
	 */
	@RequestMapping("/pageConfig/getPage")
	public void getPage(@RequestBody PageConfigDTO dto, HttpServletResponse response) {
		//如果有缓存则通过缓存获取
		String redisKey = RedisKeyConstant.APP_PAGE_CACHE + dto.hashCode();
		String pageCache = redisService.get(redisKey);
		if (!StringUtil.isEmpty(pageCache)) {
			Util.sendResponse(pageCache, response);
			log.info("调用页面缓存返回");
			return;
		}
		PageConfigDTO result = null;
		if (dto.getId() != null && dto.getId() > 0) {
			result = pageConfigService.getPageById(dto);
		} else if (dto.getType() != null) {
			result = pageConfigService.getPageByType(dto);
		} else {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		
		//序列化设置
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter((source, name, value) -> value == null);
		config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
			@Override  
			public Object processArrayValue(Object paramObject,  
			        JsonConfig paramJsonConfig) {  
			    return process(paramObject);  
			}  
			@Override  
			public Object processObjectValue(String paramString, Object paramObject,  
			        JsonConfig paramJsonConfig) {  
			    return process(paramObject);  
			}  
			private Object process(Object value){  
			    if(value instanceof Date){    
			        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);    
			        return sdf.format(value);  
			    }    
			    return value == null ? "" : value.toString();    
			}  
		});
		Response resp = new Response();
        resp.setRes_data(result);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        String resultStr = JSONObject.fromObject(resp, config).toString();
        //设置缓存过期时间为1小时
        redisService.set(redisKey, resultStr, 1L, TimeUnit.HOURS);
        log.info("页面缓存设置成功key：" + redisKey);
        Util.sendResponse(resultStr, response);
	}
	
	
}
